package com.exrobot.module.rbt.service.robotScheduler;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Schema(description = "机器人和参数信息")
@Data
public class RobotFood {

    @Schema(description = "机器人SN")
    private String sn;

    @Schema(description = "机器人楼层")
    private String floor;

    @Schema(description = "机器人携带的餐食信息")
    private Map<Long, Integer> inventory;

    public RobotFood() {
        this.inventory = new HashMap<>();
    }

    public RobotFood(String sn, String floor,Map<Long, Integer> inventory) {
        this.sn = sn;
        this.floor = floor;
        this.inventory = inventory;
    }

    public RobotFood(String sn, String floor) {
        this.sn = sn;
        this.floor = floor;
        this.inventory = new HashMap<>();
    }

    public void addItem(Long foodId, int num) {
        inventory.put(foodId, num);
    }

    public int getFoodNum(Long foodId) {
        return inventory.getOrDefault(foodId, 0);
    }

    public boolean hasEnoughFood(Long foodId, int num) {
        return getFoodNum(foodId) >= num;
    }

    public boolean canFulfillOrder(SaleOrder order) {
        for (SaleOrderItem orderItem : order.getItems()) {
            if (!hasEnoughFood(orderItem.getFoodId(), orderItem.getNum())) {
                return false;
            }
        }
        return true;
    }

    public boolean processOrder(SaleOrder order) {
        if (!canFulfillOrder(order)) {
            return false;
        }
        for (SaleOrderItem orderItem : order.getItems()) {
            Long foodId = orderItem.getFoodId();
            int num = orderItem.getNum();
            inventory.put(foodId, inventory.get(foodId) - num);
        }
        return true;
    }

    @Override
    public String toString() {
        return "Vehicle " + sn + " (Floor " + floor + ")";
    }
}


